package org.footballmanager.pandafootball.system.controller.hy;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.controller.BaseController;
import org.footballmanager.pandafootball.system.form.impl.FootLySearchForm;
import org.footballmanager.pandafootball.system.service.FootLyService;
import org.footballmanager.pandafootball.system.service.FootUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@SessionAttributes("footLySearchForm")
@RequestMapping("/hy/footLy")
public class HyLyController extends BaseController {
	/* 实体名称 */
	private static final String ENTITYNAME = "footLy";
	private static final String LISTPAGE = String.format(Constants.LISTPAGE, Constants.HY, ENTITYNAME);
	private static final String REDIRECT = String.format(Constants.REDIRECT, Constants.HY, ENTITYNAME);

	@Autowired
	private FootLyService footLyService;
	
	@Autowired
	private FootUserService footUserService;

    /**
     * 初始化
     * @param map 参数
     * @param footLySearchForm 条件参数
     */
    @ModelAttribute
    public void init(Model map, @ModelAttribute FootLySearchForm footLySearchForm){
        map.addAttribute("userList", footUserService.getall());
        FootLySearchForm newFootLySearchForm = new FootLySearchForm();
        if(footLySearchForm != null){
            newFootLySearchForm = footLySearchForm;
        }
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newFootLySearchForm);
    }

	@RequestMapping(value="/search")
	public String search(Model map, @ModelAttribute FootLySearchForm footLySearchForm) {
        footLyService.search(ENTITYNAME, map, footLySearchForm);
		return LISTPAGE;
	}

	@RequestMapping(value = "/view", method = RequestMethod.GET)
	public String openViewPage(Model map, @RequestParam Long id, HttpServletRequest request,
                       @ModelAttribute FootLySearchForm footLySearchForm) {
		map.addAttribute(ENTITYNAME, footLyService.get(id));
		return REDIRECT;
	}	
		
}
